package mock.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import mock.dto.CardDTO;
import mock.utility.Utility;


public class CardDAOImp implements CardDAO{

	private Utility util = new Utility();
	
	/**
	 * input: cardNo
	 * output: CardDTO
	 */
	@Override
	public CardDTO getCardByCardNo(String cardNo) {
		Connection con = util.createConnection();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = con.prepareStatement("select * from Card where CardNo = ?");
			ps.setString(1, cardNo);
			rs = ps.executeQuery();
			if(rs.next()){
				String cardNum = rs.getString("CardNo");
				String status = rs.getString("Status");
				int accountID = rs.getInt("AccountID");
				String PIN = rs.getString("PIN");
				Date startDate = rs.getDate("StartDate");
				Date expiredDate = rs.getDate("ExpiredDate");
				int attempt = rs.getInt("Attempt");
				CardDTO card = new CardDTO(cardNum, status, accountID, PIN, startDate, expiredDate, attempt);
				return card;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally{
			if(con != null)
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(rs != null)
				try {
					rs.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		return null;
	}

	/**
	 * input: cardNo, newAttempt
	 * output: attempt++ in database
	 */
	@Override
	public void setAttempt(String cardNo, int newAttempt) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement("update Card set Attempt = ? where CardNo = ?");
			ps.setInt(1, newAttempt);
			ps.setString(2, cardNo);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(con != null)
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}

	/**
	 * input: cardNo, status
	 * output: update status in database
	 */
	@Override
	public void setStatus(String cardNo, String status) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		PreparedStatement ps = null;
		try {
			ps = con.prepareStatement("update Card set Status = ? where CardNo = ?");
			ps.setString(1, status);
			ps.setString(2, cardNo);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(con != null)
				try {
					con.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			if(ps != null)
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
	}
	
	/**
	 * thay doi PIN
	 */
	@Override
	public boolean setPIN(String cardNo, String newPIN) {
		// TODO Auto-generated method stub
		Connection con = util.createConnection();
		if(con != null){
			PreparedStatement ps = null;
			try {
	            String query = "UPDATE Card SET PIN = ? WHERE CardNo = ?";
	            ps = con.prepareStatement(query);
	            ps.setString(1, newPIN);
	            ps.setString(2, cardNo);
	            int temp = ps.executeUpdate();
	            if( temp > 0) return true;
	        } catch (SQLException e) {
	            //e.printStackTrace();
	        	System.out.println("Khong thuc hien update duoc!");
	        }finally {
	            try {
	                if (ps != null) {
	                    ps.close();
	                }
	                if (con != null) {
	                    con.close();
	                }
	            } catch (SQLException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
	                e.printStackTrace();
	            }
	        }
		}	
        return false;
	}
	
}
